<?php
/*
	用户端公共文件，访问用户端时默认加载
*/
defined('BY_JYA') or exit('error');
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'index';
if($_RQ['op']=='index'){
	//还不是分销商，跳转申请页
	$return['apply']=$this->get('agent_area',array('mid'=>$_SESSION['uid']));
	$return['member']=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if(!$return['member']['mid']){
		$this->model('member')->register();
	}
	$return['shopmember']=pdo_get('jy_weishop_member',array('merchid'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if(!$return['shopmember']['level']){
		exi('VIP以上会员才能申请区域代理！','error');
	}
	$return['become']=$this->setting['become'];
	$return['htmls']=$this->template_url('apply');
	$return['page_setting']=$this->plugin_setting('page');
	$return['apply_setting']=$this->plugin_setting('apply');
	if(!$this->setting['no_indexurl']){
		$return['index_url']=$this->setting['index_url']?:'jy_shopmerch/index/';
	}
	$return['item']=model('core/member')->detail($_SESSION['uid'],array('nickname','avatar','realname','mobile','wechat'));	
	$return['item']['oldmobile']=$return['item']['mobile'];
	$setting=uni_setting('register');
	$return['item']['login_type']=$setting['login_type'];
	$return['item']['area']=array('duodai'=>'');
	if($_SESSION['wxapp_id'] && !$_SESSION['fromh5wxapp']){
		$return['areas']=$this->model()->available_areas(1,1);
	}else{
		$return['areas']=$this->model()->available_areas();
	}
	$return['post']=array('jy'=>1);
	$return['agent_level']=$this->setting['level'];
	$return['levels']=$this->model('model')->get_levels();
	foreach($return['levels'] as $k=>$item){
		if($item['id']>0 || $item['id']=='a0'){
			unset($return['levels'][$k]);
		}
	}
	set_html('title',$return['apply_setting']['html_title']?:'申请入驻');
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$return['shopmember']=pdo_get('jy_weishop_member',array('merchid'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if(!$return['shopmember']['level']){
		exi('VIP以上会员才能申请区域代理！','error');
	}
	if(!$post['agree']){
		exi('请先阅读协议！','error');
	}
	if(!$post['realname']){
		exi('请输入姓名！','error');
	}
	if(!$post['mobile']){
		exi('请输入联系电话！','error');
	}
	if(!$post['level_id']){
		exi('请选择申请的等级！','error');
	}
	if($post['login_type']>0 && $post['oldmobile']!=$post['mobile'] && !check_sms_code($post['smscode'])){
		$oldmobile=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'mobile');
		if($post['mobile']!=$oldmobile){
			exi('手机验证码错误！','error');
		}
	}
	//代理地区整理
	if(!$post['area'] || !$post['area']['province']){
		exi('请选择代理地区！','error');
	}
	if(!$post['area']['city'] && $post['level_id']=='a2'){
		exi('请选择代理城市！','error');
	}
	if(!$post['area']['district'] && $post['level_id']=='a3'){
		exi('请选择代理区县！','error');
	}
	if(!$post['area']['street'] && $post['level_id']=='a4'){
		exi('请选择代理街道！','error');
	}
	$default_level=$this->plugin_setting('default_level');
	if(substr($post['level_id'],0,1)=='a'){
		$price=$default_level[$post['level_id'].'_price'];
	}else{
		$price=pdo_getcolumn('jy_area_psi_level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['level_id']),'price');
	}
	$area_code=$this->model()->get_area_code($post['area']);
	$post['price']=$price;
	$this->model('member')->apply_agent($_SESSION['uid'],$area_code,$post);
	if($price>0){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'level_id'=>$post['level_id'],
			'fee'=>$price,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_area_psi_yajin_log',$insert);
		$order_id=pdo_insertid();
		$params=array(
			'id'=>$order_id,
			'title'=>'区代申请费用',
			'fee'=>$price,
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_area_psi/apply/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>$success_url?:'jy_area_psi/index/',
			'error_url'=>$error_url?:'jy_area_psi/index/',
		);
		$payid=cfc('pay')->create_pay_order($params);
		$this->pdo_update('yajin_log',array('pay_id'=>$payid),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
		$res=array(
			'pay_id'=>$payid,
			'order_id'=>$order_id,
			'success_url'=>$success_url,
			'error_url'=>$error_url,
		);
		exi($res);
	}
	if(!$this->setting['no_indexurl']){
		$url=$this->setting['index_url']?:'jy_shopmerch/index/';
	}
	exi('申请成功！请耐心等待管理员审核！','',$url);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_area_psi/index/');
	}
	$order=pdo_get('jy_area_psi_yajin_log',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_area_psi/index/');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['fee'].'|'.$payresult['fee'],'error','jy_area_psi/index/');
	}
	pdo_update('jy_area_psi_agent_area',array('status'=>1,'is_yajinfee +='=>$order['fee'],'level_id'=>$order['level_id']),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
	$this->update('member',array('is_agent'=>1),array('mid'=>$order['mid'],'is_agent'=>0,'merchid'=>$order['merchid']));
	$update=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	pdo_update('jy_area_psi_yajin_log',$update,array('id'=>$payresult['id']));
	if($order['fee']>0){
		$this->model('commission')->create_fee($order,2);
	}
	exi('提交成功!');
}
